$(document).ready(function(){

    //链接
	$(document).on("click",".rackbox",function(){
		
		var hjcode=$(this).parent().parent().parent().parent().find(".left").find(".aaa").text();
		
		var hjlocation=$(this).text();
		
		var hjnum=GetQueryString("hjnum");
		
		var brandid=GetQueryString("brandid");
		
		window.location.href="materialchoose.html?brandid="+brandid+"&hjlocation="+hjlocation+"&hjcode="+hjcode+"&hjnum="+hjnum;
	})

	//截取地址栏参数
    function GetQueryString(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return decodeURI(r[2]); return null;
    }		
	
	var brandid=GetQueryString("brandid");
	
	var hjnum=GetQueryString("hjnum");
	
	var hjarr=new Array();
	
	//加载品牌货架数据
	//loadgoodsshelves();
	
	function loadgoodsshelves(){
			$.ajax({
	            type: "post",
	            url: "/mobile/gjapp/goodsshelves/querygoodsshelves",
	            dataType:"json",
	            contentType:"application/json",
	            data: JSON.stringify({"brandid":brandid}),
	            success: function (data)
				{
					if(data.state == 0)
					{
						
						//求数据长度是否是三的倍数，余数多少
						var ys=data.data.length%3;
						
						var brandname=data.data[0].name;
						
						$(".addr").text(brandname);	
						
						$(".hjnum").text("货架数量："+hjnum);						
						
						for (var n=0;n<data.data.length;n++) {

						var strcode=data.data[n].hjstr;
						
						var hjcode=data.data[n].hjcode;
						
						hjarr=strcode.split(",");
						
						unique(hjarr);
						
						fsort(hjarr);
						
						$(".conentbox").append("<div class='one'>"+
											   		"<div class='left'>"+
											   			"<div class='text'>"+
											   				"<div class='aaa'>"+hjcode+"</div>"+
											   			"</div>"+
											   		"</div>"+	
											   		"<div class='right'>"+
											   	    "</div>"+
											    "</div>");
											    
						for (var i=0;i<hjarr.length;i++) {
							
			   				$(".right").last().append(
			   				"<div class='right-box'>"+
			   					"<div class='text'>"+
			   						"<div class='aaa rackbox'>"+hjarr[i]+"</div>"+
			   					"</div>"+
			   				"</div>	");
			   				
			   			}
						
						var h=10/hjarr.length*10-1;
						
						$(".right").last().find(".right-box").prop('style','height: '+h+"%;'");

						}
						
						
						//判断是否需要添加空doc元素占位
						if(ys!=0){
							
							for(var i=0;i<(3-ys);i++){
								$(".conentbox").append("<div class='one'>"+
													   		"<div class=''>"+
													   			"<div class=''>"+
													   				"<div class=''></div>"+
													   			"</div>"+
													   		"</div>"+	
													   		"<div class='right'>"+
													   	    "</div>"+
													    "</div>");
													    
								for (var n=0;n<1;n++) {
					   				$(".right").last().append(
									   				"<div class='right-box'>"+
									   					"<div class=''>"+
									   						"<div class=''></div>"+
									   					"</div>"+
									   				"</div>	");
					   			}
								var h=99;
								$(".right").last().find(".right-box").prop('style','height: '+h+"%;'");								
								
							}
						}						
						
						
						
					}else{
						alert(data.msg);
					}
	            }
			});	
	}
	
	//去重
	function unique(arr){            
        for(var i=0; i<arr.length; i++){
            for(var j=i+1; j<arr.length; j++){
                if(arr[i]==arr[j]){         //第一个等同于第二个，splice方法删除第二个
                    arr.splice(j,1);
                    j--;
                }
            }
        }
	    return arr;
	}
	
	//排序
	function fsort(arr) {
	    var newarr = arr;
	    /** 2、 冒泡法排序
	    * 插入发排序，即那数组的前一项和后一项对比，如果前面一项小于后面
	    * 一项，则将两者位置互换，从数组第1个元素开始对比；如下示例
	    */
	    for (let i = 0; i < newarr.length; i++) {
	        for (let j = 0; j < newarr.length; j++) {
	            if (newarr[j] > newarr[j+1]) {
	                let pre = newarr[j];
	                newarr[j] = newarr[j+1];
	                newarr[j+1] = pre;
	            };
	        };
	    };
	    return newarr;
	}
	
	/* layui的弹出层 */
	layui.use(['jquery', 'layer', 'form', 'table'], function() {
		var $ = layui.jquery;
		var layer = layui.layer;
		var form = layui.form;
		var table = layui.table;
		
		$(function(){
			$("#onclick").click(function(){
				openAddUser();
			})
		})
		
		var url;
		var mainIndex;
		//打开添加页面
		function openAddUser() {
			mainIndex = layer.open({
				type: 1,
				title: '货区管理',
				content: $("#saveOrUpdateDiv"),
				area: ['70%', '90%'],
				success: function(index) {
					//清空表单数据   
					/* $("#dataFrm")[0].reset();
					url = "user/addUser.action"; */
				}
			});
		}	
	});
	
})


